package zhongbo.java.air.monitor.web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import zhongbo.java.air.common.exception.BusinessException;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.common.vo.ResultVo;
import zhongbo.java.air.monitor.Service.AirAlertLogService;
import zhongbo.java.air.monitor.model.AirAlertLog;

import java.util.Date;

@RestController
@RequestMapping("alertLog")
@Api(tags = "告警日志接口")
public class AirAlertLogController {


    @Autowired
    private AirAlertLogService airAlertLogService;


    @ApiOperation("修改告警日志")
    @PutMapping("edit")
    public ResultVo updateFlightActivityDaily(@RequestBody AirAlertLog edit) {
        boolean isUpdate = airAlertLogService.updateById(edit);
        if (edit.getId() == null || edit.getId().isEmpty()){
            throw new BusinessException("id不存在无法进行修改");
        }
        if (!isUpdate){
            throw new BusinessException("修改有误不存在告警日志");
        }
        return ResultVo.ok("修改成功!");
    }

    @ApiOperation("添加告警日志")
    @PostMapping("add")
    public ResultVo saveFlightActivityDaily(@RequestBody AirAlertLog entity) {
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        boolean isSave = airAlertLogService.save(entity);
        if (!isSave){
            throw new BusinessException("添加失败!");
        }
        return ResultVo.ok("修改成功!");
    }

    @ApiOperation("删除告警日志")
    @DeleteMapping("delete")
    public ResultVo deleteFlightActivityDaily(String entityId) {
        boolean isDelete = airAlertLogService.removeById(entityId);
        if (!isDelete){
            throw new BusinessException("删除失败,不存该航迹数据!");
        }
        return ResultVo.ok("删除成功!");
    }

    @GetMapping("/findPage")
    @ApiOperation("分页查询")
    public ResultVo<PageVo<AirAlertLog>> page(Integer page, Integer pageSize
            , String icao, String startDate, String endDate
            , String keyword, String fields
            , String oderByField, Boolean orderByAsc){
        return ResultVo.ok(airAlertLogService.pageQuery(page, pageSize
                , icao, startDate, endDate
                , keyword, fields, oderByField, orderByAsc));
    }

    @GetMapping("findOne")
    @ApiOperation("查询一条")
    ResultVo<AirAlertLog> findOne(String entityId) {
        AirAlertLog alertLog = airAlertLogService.getById(entityId);
        return ResultVo.ok(alertLog);
    }
}
